/*
  +----------------------------------------------------------------------+
  | PHP Version 7                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2016 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifndef PHP_MYSERVER_H
#define PHP_MYSERVER_H

#include "php.h"
#include "php_ini.h"
#include "php_globals.h"
#include "php_main.h"

#include "php_streams.h"
#include "php_network.h"

#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "zend_variables.h"
#include <ext/date/php_date.h>
#include <ext/standard/url.h>
#include <ext/standard/info.h>
#include <ext/standard/php_array.h>
#include <ext/standard/basic_functions.h>
#include <ext/standard/php_http.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef SW_STATIC_COMPILATION
#include "php_config.h"
#endif

#include "swoole.h"
#include "Server.h"
#include "php7_wrapper.h"


extern zend_module_entry myserver_module_entry;

extern zend_class_entry myserver_server_ce;
extern zend_class_entry *myserver_server_class_entry_ptr;

#define php_swoole_array_get_value(ht, str, v)     (sw_zend_hash_find(ht, str, sizeof(str), (void **) &v) == SUCCESS && !ZVAL_IS_NULL(v))
#define php_swoole_array_separate(arr)       zval *_new_##arr;\
    SW_MAKE_STD_ZVAL(_new_##arr);\
    array_init(_new_##arr);\
    sw_php_array_merge(Z_ARRVAL_P(_new_##arr), Z_ARRVAL_P(arr));\
    arr = _new_##arr;

#define phpext_myserver_ptr &myserver_module_entry

#define PHP_MYSERVER_VERSION "0.1.0" /* Replace with version number for your extension */

#ifdef PHP_WIN32
#	define PHP_MYSERVER_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#	define PHP_MYSERVER_API __attribute__ ((visibility("default")))
#else
#	define PHP_MYSERVER_API
#endif

#ifdef ZTS
#include "TSRM.h"
#endif

/*
  	Declare any global variables you may need between the BEGIN
	and END macros here:

ZEND_BEGIN_MODULE_GLOBALS(myserver)
	zend_long  global_value;
	char *global_string;
ZEND_END_MODULE_GLOBALS(myserver)
*/

/* Always refer to the globals in your function as MYSERVER_G(variable).
   You are encouraged to rename these macros something shorter, see
   examples in any other php module directory.
*/
#define MYSERVER_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(myserver, v)

#if defined(ZTS) && defined(COMPILE_DL_MYSERVER)
ZEND_TSRMLS_CACHE_EXTERN()
#endif

#define SWOOLE_OBJECT_MAX       10000000
#define SWOOLE_PROPERTY_MAX      32 

//------myserver_server----
PHP_METHOD(myserver_server,__construct);
PHP_METHOD(myserver_server,__destruct);
PHP_METHOD(myserver_server,start);
PHP_METHOD(myserver_server,on);
PHP_METHOD(myserver_server,send);
PHP_METHOD(myserver_server,set);

typedef struct
{
	void **array;
	uint32_t size;
	void **property[SWOOLE_PROPERTY_MAX];
	uint32_t property_size[SWOOLE_PROPERTY_MAX];
} swoole_object_array;

#ifdef ZTS
#include "TSRM.h"
extern void ***sw_thread_ctx;
extern __thread swoole_object_array swoole_objects;
#else
extern swoole_object_array swoole_objects;
#endif

enum php_swoole_server_callback_type
{
	//--------------------------Swoole\Server--------------------------
    SW_SERVER_CB_onConnect,        //worker(event)
    SW_SERVER_CB_onReceive,        //worker(event)
    SW_SERVER_CB_onClose,          //worker(event)
    SW_SERVER_CB_onPacket,         //worker(event)
    SW_SERVER_CB_onStart,          //master
    SW_SERVER_CB_onShutdown,       //master
    SW_SERVER_CB_onWorkerStart,    //worker(event & task)
    SW_SERVER_CB_onWorkerStop,     //worker(event & task)
    SW_SERVER_CB_onTask,           //worker(task)
    SW_SERVER_CB_onFinish,         //worker(event & task)
    SW_SERVER_CB_onWorkerExit,     //worker(event)
    SW_SERVER_CB_onWorkerError,    //manager
    SW_SERVER_CB_onManagerStart,   //manager
    SW_SERVER_CB_onManagerStop,    //manager
    SW_SERVER_CB_onPipeMessage,    //worker(evnet & task)
    //--------------------------Swoole\Http\Server----------------------
    SW_SERVER_CB_onRequest,        //http server
    //--------------------------Swoole\WebSocket\Server-----------------
    SW_SERVER_CB_onHandShake,      //worker(event)
    SW_SERVER_CB_onOpen,           //worker(event)
    SW_SERVER_CB_onMessage,        //worker(event)
    //--------------------------Buffer Event----------------------------
    SW_SERVER_CB_onBufferFull,     //worker(event)
    SW_SERVER_CB_onBufferEmpty,    //worker(event)
    //-------------------------------END--------------------------------
};

#define PHP_SERVER_CALLBACK_NUM             (SW_SERVER_CB_onBufferEmpty+1)

extern zval *php_sw_server_callbacks[PHP_SERVER_CALLBACK_NUM];
extern zend_fcall_info_cache *php_sw_server_caches[PHP_SERVER_CALLBACK_NUM];

void php_swoole_onConnect(swServer *, swDataHead *);
void php_swoole_onClose(swServer *, swDataHead *);

static sw_inline zval* php_swoole_server_get_callback(swServer *serv, int server_fd, int event_type){
	return php_sw_server_callbacks[event_type];
}

static sw_inline zend_fcall_info_cache* php_swoole_server_get_cache(swServer *serv, int server_fd, int event_type){
	return php_sw_server_caches[event_type];
}

static sw_inline void* swoole_get_object(zval *object)
{
    int handle = sw_get_object_handle(object);
    assert(handle < swoole_objects.size);
    return swoole_objects.array[handle];
}

#endif	/* PHP_MYSERVER_H */


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
